<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">

            <!-- <el-form-item label="调整时间" prop="adjustTime">
        <el-date-picker clearable
          v-model="queryParams.adjustTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择调整时间">
        </el-date-picker>
      </el-form-item> -->
            <el-form-item label="计划ID" prop="adjustId">
                <el-input v-model="queryParams.adjustId" placeholder="请输入计划ID" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="申请时间" prop="applyTime">
                <el-date-picker clearable v-model="queryParams.applyTime" type="date" value-format="yyyy-MM-dd"
                    placeholder="请选择申请时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="申请人" prop="applyUser">
                <el-input v-model="queryParams.applyUser" placeholder="申请人" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['system:adjust:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['system:adjust:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete" v-hasPermi="['system:adjust:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['system:adjust:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="adjustList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="计划id" align="center" prop="adjustId" />
            <el-table-column label="申请时间" align="center" prop="applyTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="申请人" align="center" prop="applyUser" />
            <el-table-column label="调整时间" align="center" prop="adjustTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.adjustTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="调整原因" align="center" prop="adjustReason" />
            <el-table-column label="后续计划是否自动调整" align="center" prop="plan" />
            <el-table-column label="审批人" align="center" prop="userId" />
            <el-table-column label="审批意见" align="center" prop="approOpinion" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="160">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['system:adjust:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['system:adjust:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />

        <!-- 添加或修改计划调整记录对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="申请时间" prop="applyTime">
                            <el-date-picker clearable v-model="form.applyTime" type="date"
                                value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择申请时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="申请人" prop="applyUser">
                            <el-input v-model="form.applyUser" placeholder="请输入申请人" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="调整时间" prop="adjustTime">
                            <el-date-picker clearable v-model="form.adjustTime" type="date"
                                value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择调整时间">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row>
                    <el-col :span="12">
                        <el-form-item label="调整原因" prop="adjustReason">
                            <el-input v-model="form.adjustReason" placeholder="请输入调整原因" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="自动调整" prop="plan">
                            <el-input v-model="form.plan" placeholder="请输入是否" />
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-form-item label="审批人" prop="userId">
                    <el-input v-model="form.userId" placeholder="请输入审批人" />
                </el-form-item>
                <el-form-item label="审批意见" prop="approOpinion">
                    <el-input v-model="form.approOpinion" placeholder="请输入审批意见" />
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listAdjust, getAdjust, delAdjust, addAdjust, updateAdjust } from "@/api/system/adjust";
import { listPlan } from "@/api/system/plan";

export default {
    name: "Adjust",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 计划调整记录表格数据
            adjustList: [],
            // 项目计划表格数据
            planList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                applyTime: null,
                applyUser: null,
                adjustTime: null,
                adjustReason: null,
                plan: null,
                userId: null,
                approOpinion: null
            },
            // 表单参数
            form: {},
            // 表单验证
            rules: {
                applyTime: [
                    { required: true, message: "申请时间不能为空", trigger: "blur" },
                ],
                applyUser: [
                    { required: true, message: "申请人不能为空", trigger: "blur" }
                ],
                adjustTime: [
                    { required: true, message: "调整时间不能为空", trigger: "blur" }
                ],
                adjustReason: [
                    { required: true, message: "调整原因不能为空", trigger: "blur" }
                ],
                plan: [
                    { required: true, message: "自动调整不能为空", trigger: "blur" }
                ],
                userId: [
                    { required: true, message: "审批人不能为空", trigger: "blur" }
                ],
                approOpinion: [
                    { required: true, message: "审批意见不能为空", trigger: "blur" }
                ],
            },
        };
    },
    created() {
        this.getList();
        this.getListPlan();
    },
    methods: {
        /** 查询项目计划列表 */
        getListPlan() {
            listPlan(this.queryParams).then(response => {
                this.planList = response.rows;
            });
        },
        /** 查询计划调整记录列表 */
        getList() {
            this.loading = true;
            listAdjust(this.queryParams).then(response => {
                this.adjustList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                adjustId: null,
                applyTime: null,
                applyUser: null,
                adjustTime: null,
                adjustReason: null,
                plan: null,
                userId: null,
                approOpinion: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.adjustId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加计划调整记录";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const adjustId = row.adjustId || this.ids
            getAdjust(adjustId).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改计划调整记录";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                console.log(this.form);
                if (valid) {
                    if (this.form.adjustId != null) {
                        updateAdjust(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addAdjust(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const adjustIds = row.adjustId || this.ids;
            this.$modal.confirm('是否确认删除计划调整记录编号为"' + adjustIds + '"的数据项？').then(function () {
                return delAdjust(adjustIds);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('system/adjust/export', {
                ...this.queryParams
            }, `adjust_${new Date().getTime()}.xlsx`)
        }
    }
};
</script>